#include "readallfiles.h"
#include "ui_readallfiles.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QDir>
#include <QImage>
#include <QFile>
#include "bindata.h"

ReadAllFiles::ReadAllFiles(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ReadAllFiles)
{
    ui->setupUi(this);

    message = new QMessageBox(this);

    connect(ui->btnOpen, SIGNAL(clicked()), this, SLOT(openFile()));
    connect(ui->btnRead, SIGNAL(clicked()), this, SLOT(readFiles()));
}

ReadAllFiles::~ReadAllFiles()
{
    delete ui;
}


void ReadAllFiles::openFile(void)
{
    strPath = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                     "/home",
                                                     QFileDialog::ShowDirsOnly
                                                     | QFileDialog::DontResolveSymlinks);

    ui->editName->setText(strPath);
}

void ReadAllFiles::readFiles(void)
{
    if (strPath.isNull())
    {
        QMessageBox::information(this,
                                 QString("错误"),QString("请选择路径"),
                                 QMessageBox::Ok);
        return;
    }

    QDir *dir = new QDir(strPath);
    QStringList filter;
    filter << "*.bmp";
    dir->setNameFilters(filter);
    QList<QFileInfo> *fileInfo = new QList<QFileInfo>(dir->entryInfoList(filter));

    strInfo.setNum(fileInfo->size());
    ui->editFileInfo->setText("#define LOGO_NUM    " + strInfo);

    QFile file(strPath+ "/outImage.bin");
    file.open(QIODevice::WriteOnly | QIODevice::Truncate);
    QDataStream out(&file);
    QByteArray readBinData;
    imgOffset = 0;

    for (qint8 i = 0; i < fileInfo->size(); i++)
    {
        readBinData = getBmpColor_rgb565(strPath+'/'+fileInfo->at(i).fileName());
        out << readBinData;

        ui->editFileInfo->append("#define ADDR_"
                                 + fileInfo->at(i).baseName().toUpper()
                                 + "    0X"
                                 + QString::number(imgOffset, 16));
        imgOffset = imgOffset + readBinData.count();
    }
    file.close();
}

/* 图片存储格式：
 * 图片宽度低
 * 图片宽度高
 * 图片高度低
 * 图片高度高
 * DATA低
 * DATA高
 */

QByteArray ReadAllFiles::getBmpColor_rgb565(QString fileName)
{
    QByteArray array("");

    qint64 red,blue,green;
    qint64 color;
    qint16 result;
    QImage *openImg = new QImage(fileName);
    imgHeight = openImg->height();
    imgWidth = openImg->width();

    array += imgWidth;
    array += imgWidth >> 8;
    array += imgHeight;
    array += imgHeight >>8;

    for (qint16 i = 0; i < imgHeight; i++)
    {
        for (qint16 j = 0; j < imgWidth; j++)
        {
            red = qRed(openImg->pixel(j, i));
            green = qGreen(openImg->pixel(j, i));
            blue = qBlue(openImg->pixel(j, i));
            color = 0;
            color = (red << 16)|(green << 8)|(blue);
            result = qint16(((color>>8)&0xf800)|((color>>5)&0x07e0)|((color>>3)&0x001f));
            array += result;
            array += result >> 8;
        }
    }
    return array;
}
